package cn.ekuma.epos.security;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.openbravo.bean.shard.UserPermission;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.security.bean.I_Permission;
import cn.ekuma.security.bean.I_Role;
import cn.ekuma.security.bean.I_RolePermission;
import cn.ekuma.security.datalogic.I_DataLogicSecurity;
import cn.ekuma.security.impl.AbstractSecurityManager;

public class OldPOSSecurityManager extends AbstractSecurityManager {
	I_DataLogicSystem dlSystem;
	private SAXParser m_sp = null;

	static OldPOSSecurityManager INSTANCE;
	
	public OldPOSSecurityManager(I_DataLogicSecurity dlSecurity,I_DataLogicSystem dlSystem) {
		super(dlSecurity, "default null");
		this.dlSystem=dlSystem;
	}

	
	public static OldPOSSecurityManager getInstance(I_DataLogicSecurity dlSecurity,I_DataLogicSystem dlSystem){
		if(INSTANCE==null)
			INSTANCE=new OldPOSSecurityManager(dlSecurity,dlSystem);
		return INSTANCE;
	}
	
	
	
	
	@Override
	public void init(I_Role role) {
		if(this.role!=null&&this.role.getID().equals(role.getID()))
			return;
		super.init(role);
	}


	@Override
	public I_RolePermission fillPermissionByRole(I_Role role) {
		BaseRolePermission baseRolePermission=new BaseRolePermission();
			String dbPermission=dlSystem.findRolePermissions(role.getID());
			List<I_Permission> ps=formatPermissions(dbPermission);
			System.out.println("find role permission by roleId:"+role.getID()+"\t total:"+ps.size()+"\n pers:"+dbPermission);
			baseRolePermission.setPermissions(ps);
		return baseRolePermission;
	}

	@Override
	public boolean hasChildPermissionByName(String parentPermission,
			String childPermisson) {
		// TODO Auto-generated method stub
		return false;
	}
	
	
	 private static class ConfigurationHandler extends DefaultHandler {
         List<I_Permission> apermissions;
        public ConfigurationHandler( List<I_Permission> m_apermissions){
            apermissions=m_apermissions;
        }
        @Override
        public void startDocument() throws SAXException {}
        @Override
        public void endDocument() throws SAXException {}
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
            if ("class".equals(qName)){
            	String value=attributes.getValue("id");
                UserPermission temp=new UserPermission(value==null?9999:Integer.valueOf(value),attributes.getValue("name"));
                temp.setGroup(attributes.getValue("group"));
                apermissions.add(temp);
            }
        }
        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {}
        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {}
    }
	 
	 
	 
	 public  List<I_Permission> formatPermissions(String sRolePermisions) {
	        // inicializamos los permisos
	       List<I_Permission> m_apermissions = new ArrayList<I_Permission>();
	       m_apermissions.add(new UserPermission(1000,"com.openbravo.pos.forms.JPanelMenu",UserPermission.PERMISSIONGROUP_System));
	       m_apermissions.add(new UserPermission(1001,"Menu.Exit",UserPermission.PERMISSIONGROUP_System));
	        if (sRolePermisions != null) {
	            try {
	                if (m_sp == null) {
	                    SAXParserFactory spf = SAXParserFactory.newInstance();
	                    m_sp = spf.newSAXParser();
	                }
	              StringReader ss=  new StringReader(sRolePermisions);
	              ConfigurationHandler configHandler=new ConfigurationHandler(m_apermissions);
	              m_sp.parse(new InputSource(ss), configHandler);
	            } catch (ParserConfigurationException ePC) {
	                //logger.log(Level.WARNING, LocalRes.getIntString("exception.parserconfig"), ePC);
	            } catch (SAXException eSAX) {
	                //logger.log(Level.WARNING, LocalRes.getIntString("exception.xmlfile"), eSAX);
	            } catch (IOException eIO) {
	                //logger.log(Level.WARNING, LocalRes.getIntString("exception.iofile"), eIO);
	            }
	        }
	        return m_apermissions;
	     } 
}
